<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="78px"
    >
      <el-form-item label="设备" class="small-label" prop="equipmentId">
        <el-select size="small" v-model="queryParams.equipmentId" placeholder="请选择设备">
          <el-option
            v-for="item in equipmentInfoList"
            :key="item.equipmentId"
            :label="item.equipmentName"
            :value="item.equipmentId"
          >
            <span style="float: left">{{ item.equipmentName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">ID:{{ item.equipmentId }}</span>
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="报警时间" label-width="120px" prop="alarmStartTime">
        <el-date-picker
        size="small"
          v-model="alarmTimeRange"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="报警类型" prop="alarmTypeId">
        <el-select
          v-model="queryEquipmentAlarmTypeIds"
          placeholder="请选择报警类型"
          clearable
          multiple
          size="small"
          @change="selectionAlarmTypeChanged"
        >
          <el-option
            v-for="dict in alarmTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          :loading="btnLoading"
          @click="handleExport"
          v-hasPermi="['equipment:alarmData:export']"
        >导出</el-button>
      </el-col>
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table :cell-style="{padding:this.tablePadding}" border v-loading="loading" :data="alarmDataList">
      <template slot="empty">
        <div>
          <span>未选择设备或无报警数据</span>
        </div>
      </template>
      <el-table-column label="设备名称" align="center" prop="alarmEquipmentName" />
      <el-table-column label="报警时间" align="center" prop="alarmTime" width="180">
      </el-table-column>
       <el-table-column label="报警类型" align="center" :formatter="typeFormat" prop="alarmTypeId">
    </el-table-column>
      <el-table-column label="报警信息" align="center" prop="alarmMessage">
    </el-table-column>
    <el-table-column label="报警状态" align="center" :formatter="statusFormat" prop="alarmValue">
    </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listAlarmData,
  getAlarmData,
  delAlarmData
} from "@/api/equipment/alarmData";

import { listInfo } from "@/api/equipment/info";

export default {
  name: "AlarmData",
  components: {},
  data() {
    return {
      btnLoading:false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报警数据表格数据
      alarmDataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 报警类型列表
      alarmTypeOptions: [],

     // 查报警参数询列表
      queryEquipmentAlarmTypeIds: [],
      pickerOptions: {
          shortcuts: [
            {
            text: '今天',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
             start.setHours('0')
             start.setMinutes('0')
             start.setSeconds('0')
              // start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
              picker.$emit('pick', [start, end]);
            }
          },{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
      // 报警时间范围
      alarmTimeRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentId: null,
        startTime: null,
        endTime: null,
        alarmTypeIds: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      equipmentInfoList: [],
      alarmStatusOptions:[]
    };
  },
  mounted() {
    this.getList();
    this.getEquipmentInfoList();
  //  报警类型
    this.getDicts("equipment_alarm_type").then(response => {
      this.alarmTypeOptions = response.data;
    });
    // 报警状态
    this.getDicts("equipment_alarm_status").then(response => {
      this.alarmStatusOptions = response.data;
    })
  },
   watch: {
    $route: {
      handler: function (route) {
        this.queryParams.equipmentId = route.query.equipmentId?parseInt(route.query.equipmentId):"";
        this.getList();
      },
      immediate: true,
    },
  },
  methods: {
     typeFormat(row, column) {
      return this.selectDictLabel(this.alarmTypeOptions, row.alarmTypeId);
    },
    statusFormat(row, column) {
      return this.selectDictLabel(this.alarmStatusOptions, row.alarmValue);
    },
    /** 查询报警数据列表 */
    getList() {
      this.loading = true;

      listAlarmData(this.queryParams).then(response => {
        this.alarmDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getEquipmentInfoList() {
      this.loading = true;
      listInfo({status:1}).then(response => {
        this.equipmentInfoList = response.rows;
        this.loading = false;
      });
    },
    selectionAlarmTypeChanged(selected) {
        this.queryParams.alarmTypeIds = selected.toString();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.startTime = this.alarmTimeRange[0];
      this.queryParams.endTime = this.alarmTimeRange[1];
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.equipmentId=''
      this.alarmTimeRange = [];
      this.queryEquipmentAlarmTypeIds = null;
      this.queryParams.alarmTypeIds = null;
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报警数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const alarmDataId = row.alarmDataId || this.ids;
      getAlarmData(alarmDataId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报警数据";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const alarmDataIds = row.alarmDataId || this.ids;
      this.$confirm(
        '是否确认删除报警数据编号为"' + alarmDataIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",closeOnClickModal:false,
          type: "warning"
        }
      )
        .then(function() {
          return delAlarmData(alarmDataIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.btnLoading=true
      this.download(
        "equipment/alarmData/export",
        {
          ...this.queryParams
        },
        `equipment_alarmData.xlsx`,
        '',
        ()=>{
          this.btnLoading=false
        }
      );
    }
  }
};
</script>
<style  scoped>
.small-label >>> .el-form-item__label {
  width:45px !important;
}
</style>